package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.exception.CustomException;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Spu;
import com.changgou.goods.service.SpuService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/spu")
public class SpuController {
    @Autowired
    private SpuService spuService;

    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size) throws Exception {
        Page<Spu> pageList = spuService.findPage(searchMap, page, size);
        if(pageList.size()==0){
            throw new CustomException(StatusCode.MESSAGE_BACD,"您查询的数据暂无，请联系管理核对！！！");
        }
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 更新添加商品
     *
     */
    @GetMapping(value = "/spuAdd")
    public Result spuAdd(@RequestBody Spu spu){
        Integer index = spuService.spuAdd(spu);
        if (index>0) {
            return new Result(true,StatusCode.OK,"商品更新成功");
        }

        return new Result(false,StatusCode.ERROR,"商品更新失败");
    }

    /**
     * 第一步添加商品
     * zxq
     * @return
     */
    @PostMapping
    public Result addFirst(@RequestBody Spu spu){
        try {
            //调用方法
            spuService.addFirst(spu);
            return new Result(true,StatusCode.OK,"第一步添加商品成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"第一步添加商品失败");
        }
    }

    /**
     * 商品上下架
     * @param id
     * @return
     */
    @GetMapping("/updata_goods_online/{id}")
    public Result updata_goods_online(@PathVariable Long id){
        int count = spuService.updataOnline(id);
        if (count>0){
            return new Result(true,StatusCode.OK,"修改商品成功");
        }
        return new Result(false,StatusCode.ERROR,"修改商品失败");
    }

    /**
     * 商品对应分类的规格参数(模板)查询_参数查询
     * @param id
     * @return
     */
    @GetMapping("/findTemplateById/{id}")
    public Result findTemplateById(@PathVariable Long id){
        List<String> list = spuService.findTemplateById(id);
        if(list != null && list.size()>0) {
            return new Result(true,StatusCode.OK,"查询成功",list);
        }
        return new Result(false,StatusCode.ERROR,"查询失败");
    }

    /**
     * 商品状态查询分页条件查询活动列表
     * @param page
     * @param size
     * @param is_marketable
     * @param status
     * @return
     */
    @GetMapping("/{page}/{size}")
    public Result findByPage(@PathVariable("page") int page,@PathVariable("size") int size,@RequestParam("is_marketable") String is_marketable,@RequestParam("status") String status){
        try {
            Map<String,Object> map = spuService.findByPage(page,size,is_marketable,status);
            return new Result(true,StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }


    //添加商品
    @PostMapping
    public Result add(@RequestBody Goods goods){
        spuService.add(goods);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    //删除商品
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        spuService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

}
